<?php
namespace app\api\model;
use app\admin\event\SystemLog;
use app\admin\model\Admin;
use think\db\Query;
use think\exception\DbException;
use think\Model;
use think\Db;

class PrepareModel extends Base
{

    protected static function init()
    {
        self::observe(SystemLog::class);
    }

    protected $table ='jxc_prepare';

    // 定义全局的查询范围
    protected function base($query)
    {
        $c = Admin::find(\app\http\GetUid::getUId());
        // 门店负责人可查看自己门店的所有信息
        if ($c->level == 2) {
            $maker = Admin::where('store', $c->store)->where('level', 3)->column('id');
        } elseif ($c->level == 1) {
            $maker = Admin::column('id');
        } else {
            $maker = $c->id;
        }
        $query->whereIn('maker', $maker);
    }
    /**
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPrepareList($data)
    {
        $where =[];
        if (!empty($data['status'])) {
            $where[] =['status','=',$data['status']];
        }
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where[] = ['create_time','between', [$data['start_time'], $data['end_time']]];
        }
        if (!empty($data['warehouse'])) {
            $where[] =['warehouse','=',$data['warehouse']];
        }
        if (!empty($data['keeper'])) {
            $where[] =['keeper','=',$data['keeper']];
        }
        if (!empty($data['number'])) {
            $where[] = ['number','like', "%" . $data['number'] . "%"];
        }
        $list =$this->getPage('prepare','id,status,number,create_time,store,warehouse,keeper,mode,des',$where,'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d',$v['create_time']);
            $list['data'][$k]['store'] =$this->getValue('store',['id'=>$v['store']],'name');
            $list['data'][$k]['warehouse'] =$this->getValue('warehouse',['id'=>$v['warehouse']],'name');
            $list['data'][$k]['keeper'] =$this->getValue('keeper',['id'=>$v['keeper']],'name');
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status1'] = "部分出库";
                    $list['data'][$k]['status2'] =13;
                    break;
                case 2:
                    $list['data'][$k]['status1'] = "已撤销";
                    $list['data'][$k]['status2'] =10;
                    break;
                case 3:
                    $list['data'][$k]['status1'] = "草稿";
                    $list['data'][$k]['status2'] =11;
                    break;
                case 4:
                    $list['data'][$k]['status1'] = "已配货";
                    $list['data'][$k]['status2'] =12;
                    break;
                default:
                    $list['data'][$k]['status1'] = "待出库";
                    $list['data'][$k]['status2'] =9;
            }
        }
        return $list;
    }
    /**
     * 配货单 新增
     * @param $data
     * @return int|string
     */
    public function getPrepareAdd($data)
    {
        $arr =$this::create($data);
        if($data['status'] ==5){
            $stock['status'] =1;
            $stock['number'] =$data['number'];
            $stock['create_time'] =$data['create_time'];
            $stock['warehouse'] =$data['warehouse'];
            $stock['maker'] =$data['maker'];
            $stock['type'] =2;//单据类型 1销售单 2配货单 3调拨单
            $stock['des'] =$data['des'];
            $stock['relation_id'] =$arr->id;
            $this->getAdd('outstock',$stock);
            $this->getSetField('prepare',['id'=>$arr->id],['relation_number'=>$stock['number']]);
            foreach ($data['goods'] as $k => $v) {
                $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
                $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
                $relation['product_id'] = $v['product_id'];
                $relation['put_number'] = $v['put_number'];
                $relation['pid'] = $arr->id;
                $relation['money'] = $relation['put_number'] * $trade;
                $this->getAdd('preparerelation',$relation);
                $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
                if(empty($stockInfo)){
                    $rel =[
                        'commodity_id' => $dan,
                        'product_id' => $v['product_id'],
                        'warehouse' => $data['warehouse'],
                        'put_out' => $v['put_number']
                    ];
                    $this->getAdd('product_stock', $rel);
                }else{
                    $this->getSetInc('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']],'put_out',$v['put_number']);
                }
            }
        }else{
            foreach ($data['goods'] as $k => $v) {
                $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
                $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
                $relation['product_id'] = $v['product_id'];
                $relation['put_number'] = $v['put_number'];
                $relation['pid'] = $arr->id;
                $relation['money'] = $relation['put_number'] * $trade;
                $this->getAdd('preparerelation',$relation);
                $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
                if(empty($stockInfo)){
                    $rel =[
                        'commodity_id' => $dan,
                        'product_id' => $v['product_id'],
                        'warehouse' => $data['warehouse'],
                        'stock' => 0
                    ];
                    $this->getAdd('product_stock', $rel);
                }
            }
        }
        return $arr;
    }

    /**
     * @param $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPrepareInfo($id)
    {
        $list = $this->getOne('prepare','*',['id'=>$id]);
        $list['goods'] = $this->getselectList('preparerelation','*',['pid'=>$list['id']]);
        foreach($list['goods'] as $k=>$v){
            $dan =$this->getOne('product','commodity_id,color,size',['id'=>$v['product_id']]);
            $goods =$this->getOne('commodity','name,item,goods_code,unit,trade_price',['id'=>$dan['commodity_id']]);
            $list['goods'][$k]['commodity_id'] =$dan['commodity_id'];
            $list['goods'][$k]['name'] =$goods['name'];
            $list['goods'][$k]['item'] =$goods['item'];
            $list['goods'][$k]['goods_code'] =$goods['goods_code'];
            $list['goods'][$k]['color'] =$dan['color'];
            $list['goods'][$k]['size'] =$dan['size'];
            $list['goods'][$k]['color_name'] =$this->getValue('color3',['id'=>$dan['color']],'name');
            $list['goods'][$k]['size_name'] =$this->getValue('color3',['id'=>$dan['size']],'name');
            $list['goods'][$k]['unit'] =$this->getValue('color1',['id'=>$goods['unit']],'name');
            $list['goods'][$k]['price'] =$goods['trade_price'];
        }
        $list['warehouse_name'] =$this->getValue('warehouse',['id'=>$list['warehouse']],'name');
        $list['store_name'] =$this->getValue('store',['id'=>$list['store']],'name');
        $list['keeper_name'] =$this->getValue('keeper',['id'=>$list['keeper']],'name');
        $list['create_time'] = date('Y-m-d', $list['create_time']);
        $list['maker_name'] =$this->getValue('admin',['id'=>$list['maker']],'name');
        $list['images'] =json_decode($list['images'],true);
        $list['mode_address'] =json_decode($list['mode_address'],true);
        if(in_array($list['mode'],['电子单面','发物流'])){
            if(isset($list['mode_address']['logistics'])){
                $list['mode_address']['logistics'] = (int)$list['mode_address']['logistics'];
                $list['mode_address']['logistics_name'] =$this->getValue('logistics',['id'=>$list['mode_address']['logistics']],'name');
            }
            if(isset($list['mode_address']['networks'])){
                $list['mode_address']['networks'] = (int)$list['mode_address']['networks'];
                $list['mode_address']['networks_name'] =$this->getValue('logistics_outlets',['id'=>$list['mode_address']['networks']],'name');
            }
        }
        switch ($list['status']){
            case 1:
                $list['status1'] = "部分出库";
                break;
            case 2:
                $list['status1'] = "已撤销";
                break;
            case 3:
                $list['status1'] = "草稿";
                break;
            case 4:
                $list['status1'] = "已配货";
                break;
            default:
                $list['status1'] = "待出库";
        }
        return $list;
    }

    /**
     * @param $id
     * @return array|int|\PDOStatement|string|Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function getPrepareDel($id)
    {
        try{
            $list =$this->getOne('prepare','*',['id'=>$id]);
            if(in_array($list['status'],[1,2,4])){
                throw new \Exception('单据已出库,无法删除');
            }else{
                if($list['status'] ==5){
                    Db::name('outstock')->where(['type'=>2,'relation_id'=>$id])->delete();
                }
                Db::name('preparerelation')->whereIn('pid',$id)->delete();
                return $this->getDel('prepare',$id);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    public function getPrepareDraft($id,$data)
    {
        $list = $this->getEdit('prepare',['id'=>$id],$data);
        Db::name('preparerelation')->whereIn('pid',$id)->delete();
        foreach ($data['goods'] as $k=>$v){
            $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
            $relation['product_id'] = $v['product_id'];
            $relation['put_number'] = $v['put_number'];
            $relation['pid'] = $id;
            $relation['money'] = $v['put_number'] * $trade;
            $this->getAdd('preparerelation',$relation);
            $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
            if(empty($stockInfo)){
                $rel =[
                    'commodity_id' => $dan,
                    'product_id' => $v['product_id'],
                    'warehouse' => $data['warehouse'],
                    'stock' => 0
                ];
                $this->getAdd('product_stock', $rel);
            }
        }
        return $list;
    }

    public function getPutStock($id,$data)
    {
        $this->getEdit('prepare',['id'=>$id],$data);
        Db::name('preparerelation')->whereIn('pid',$id)->delete();
        foreach ($data['goods'] as $k=>$v){
            $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
            $relation['product_id'] = $v['product_id'];
            $relation['put_number'] = $v['put_number'];
            $relation['pid'] = $id;
            $relation['money'] = $relation['put_number'] * $trade;
            $this->getAdd('preparerelation',$relation);
            $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
            if(empty($stockInfo)){
                $rel =[
                    'commodity_id' => $dan,
                    'product_id' => $v['product_id'],
                    'warehouse' => $data['warehouse'],
                    'put_out' => $v['put_number']
                ];
                $this->getAdd('product_stock', $rel);
            }else{
                $this->getSetInc('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']],'put_out',$v['put_number']);
            }
        }
        $list =$this->getOne('prepare','*',['id'=>$id]);
        $stock['status'] =1;
        $stock['number'] =$list['number'];
        $stock['create_time'] =$list['create_time'];
        $stock['warehouse'] =$list['warehouse'];
        $stock['maker'] =$list['maker'];
        $stock['type'] =2;//单据类型 1销售单 2配货单 3调拨单
        $stock['des'] =$list['des'];
        $stock['relation_id'] =$id;
        return $this->getAdd('outstock',$stock);
    }

    /**
     * 配货订单  撤销
     * @param $id
     * @return array|int|\PDOStatement|string|Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function getPrepareRevoke($id)
    {
        try{
            $list =$this->getOne('prepare','*',['id'=>$id]);
            if(in_array($list['status'],[1,4])){
                throw new \Exception('单据已出库,无法撤销');
            }else{
                if($list['status'] ==5){
                    $this->getSetField('outstock',array('relation_id'=>$id,'type'=>2),['status'=>4]);
                }
                return $this->getSetField('prepare',['id'=>$id],['status'=>2]);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * 配货订单 ----出库
     * @param $id
     * @param $option
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    /*public function getDepot($id, $option)
    {
        $list = $this->getOne('prepare','*',['id'=>$id]);
        $list['goods'] = $this->getselectList('preparerelation','*',['pid'=>$list['id']]);
        foreach($list['goods'] as $k=>$v){
            foreach($option as $k1=>$v1){
                if($v['id'] == $v1['id']){
                    $list['goods'][$k]['num'] =$v1['num'];
                }
            }
        }
        $arr =[];
        foreach($list['goods'] as $k=>$v){
            $yu =$v['put_number']-$v['number'];//剩余待出库数量
            if($v['num'] >$yu){
                //输入的数量大于剩余待出库数量
                throw new \Exception('超出库存上限');
            }else if($v['num'] == $yu){
                //输入的数量等于剩余待出库数量 全部出库
                $arr[]=1;
            }else{
                //输入的数量小于剩余待出库数量 部分出库
                $arr[]=2;
            }
            $this->getSetInc('preparerelation',['id'=>$v['id']],'number',$v['num']);
            $this->getSetDec('product_stock',['product_id'=>$v['product_id']],'stock',$v['num']);
            $goods =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $this->getSetDec('warehouse_goods',['warehouse_id'=>$list['warehouse'],'goods_id'=>$goods],'stock',$v['num']);
            $stock =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $flow =[
                'goods_id' =>$v['product_id'],
                'create_time' =>time(),
                'warehouse' =>$list['warehouse'],
                'type' =>'配货单',
                'number' =>$list['number'],
                'add_num' =>'-'.$v['num'],
                'sur_num' =>$stock - $v['num']
            ];
            $this->getAdd('flowing_water',$flow);
        }
        if(array_sum(array_unique($arr)) ==1){
            //全部出库
            $this->getSetField('prepare',['id'=>$id],['status'=>4]);
            $this->getSetField('outstock',['relation_id'=>$id,'type'=>2],['status'=>3]);
        }else{
            //部分出库
            $this->getSetField('prepare',['id'=>$id],['status'=>1]);
            $this->getSetField('outstock',['relation_id'=>$id,'type'=>2],['status'=>1]);
        }
        return 1;
    }*/

    /**
     * 全部出库
     * @param $id
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllDepot($id)
    {
        $list = $this->getOne('prepare','*',['id'=>$id]);
        $list['goods'] = $this->getselectList('preparerelation','*',['pid'=>$list['id']]);
        foreach ($list['goods'] as $k => $v) {
            $this->getSetField('preparerelation',['id'=>$v['id']],['number',$v['put_number']]);
            $this->getSetDec('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$list['warehouse']],'stock',$v['put_number']);
            $this->getSetDec('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$list['warehouse']],'put_out',$v['put_number']);
            $stock =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $flow =[
                'goods_id' =>$v['product_id'],
                'create_time' =>time(),
                'warehouse' =>$list['warehouse'],
                'type' =>'配货单',
                'number' =>$list['number'],
                'add_num' =>'-'.$v['put_number'],
                'sur_num' =>$stock - $v['put_number']
            ];
            $this->getAdd('flowing_water',$flow);
        }
        $payment =[
            'customer_id' => $list['store'],
            'customer_type' => 'store',
            'maker' => $list['maker'],
            'in_amount' => $list['total'],
            'des' => $list['des'],
            'create_time' => time(),
            'images' => $list['images'],
            'business_type' => '配货单',
            'business_number' => $list['number'],
            'business_time' => $list['create_time'],
        ];
        $this->getAdd('set_account_flow', $payment);
        $this->getSetField('outstock',array('relation_id'=>$list['id'],'type'=>2),['status'=>3]);
        $this->getSetField('prepare',['id'=>$list['id']],['status'=>4]);
        return 1;
    }

    /**
     * 配货退货单
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getReturnList($data)
    {
        $where =[];
        if (!empty($data['status'])) {
            $where[] =['status','=',$data['status']];
        }
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where[] = ['create_time','between', [$data['start_time'], $data['end_time']]];
        }
        if (!empty($data['keeper'])) {
            $where[] =['keeper','=',$data['keeper']];
        }
        if (!empty($data['warehouse'])) {
            $where[] =['warehouse','=',$data['warehouse']];
        }
        if (!empty($data['store'])) {
            $where[] =['store','=',$data['store']];
        }
        if (!empty($data['number'])) {
            $where[] = ['number','like', "%" . $data['number'] . "%"];
        }
        $list =$this->getPage('preparereturn','id,status,number,create_time,store,warehouse,keeper,des',$where,'id desc',$data['page'],$data['size']);
        $list =$list->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d',$v['create_time']);
            $list['data'][$k]['store'] =$this->getValue('store',['id'=>$v['store']],'name');
            $list['data'][$k]['warehouse'] =$this->getValue('warehouse',['id'=>$v['warehouse']],'name');
            $list['data'][$k]['keeper'] =$this->getValue('keeper',['id'=>$v['keeper']],'name');
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status1'] = "已退货";
                    $list['data'][$k]['status2'] = 14;
                    break;
                case 2:
                    $list['data'][$k]['status1'] = "已撤销";
                    $list['data'][$k]['status2'] = 15;
                    break;
                default:
                    $list['data'][$k]['status1'] = "草稿";
                    $list['data'][$k]['status2'] = 16;
            }
        }
        return $list;
    }

    /**
     * 配货退货 新增
     * @param $data
     * @return int|string
     */
    public function getReturnAdd($data)
    {
        $arr1 = $this->getAdd('preparereturn',$data);
        if($data['status'] ==1){
            $stock['status'] =3;
            $stock['number'] =$data['number'];
            $stock['create_time'] =$data['create_time'];
            $stock['warehouse'] =$data['warehouse'];
            $stock['maker'] =$data['maker'];
            $stock['type'] =5;//单据类型  5 配货退货单
            $stock['des'] =$data['des'];
            $stock['relation_id'] =$arr1;
            $this->getAdd('instock',$stock);
            foreach ($data['goods'] as $k => $v) {
                $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
                $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
                $arr['goods_status'] = $v['goods_status'];
                $arr['product_id'] = $v['product_id'];
                $arr['put_number'] = $v['put_number'];
                $arr['number'] = $v['put_number'];
                $arr['pid'] =$arr1;
                $arr['money'] = $v['put_number'] * $trade;
                $this->getAdd('preparereturnrelation',$arr);
                $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
                $flow =[
                    'goods_id' =>$v['product_id'],
                    'create_time' =>time(),
                    'warehouse' =>$data['warehouse'],
                    'type' =>'配货退货单',
                    'number' =>$data['number'],
                    'add_num' =>$v['put_number'],
                    'sur_num' =>(isset($stockInfo['stock']) ? $stockInfo['stock'] : 0) + $v['put_number']
                ];
                $this->getAdd('flowing_water',$flow);
                if(empty($stockInfo)){
                    $rel =[
                        'commodity_id' => $dan,
                        'product_id' => $v['product_id'],
                        'warehouse' => $data['warehouse'],
                        'stock' => $v['put_number']
                    ];
                    $this->getAdd('product_stock', $rel);
                }else{
                    $this->getSetInc('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']],'stock',$v['put_number']);
                }
            }
            $payment =[
                'customer_id' => $data['store'],
                'customer_type' => 'store',
                'maker' => $data['maker'],
                'out_amount' => $data['total'],
                'des' => $data['des'],
                'create_time' => time(),
                'images' => $data['images'],
                'business_type' => '配货退货单',
                'business_number' => $data['number'],
                'business_time' => $data['create_time'],
            ];
            $this->getAdd('set_account_flow', $payment);
        }else{
            foreach ($data['goods'] as $k => $v) {
                $dan = $this->getValue('product', ['id' => $v['product_id']], 'commodity_id');
                $trade = $this->getValue('commodity', ['id' => $dan], 'trade_price');
                $arr['goods_status'] = $v['goods_status'];
                $arr['product_id'] = $v['product_id'];
                $arr['put_number'] = $v['put_number'];
                $arr['pid'] = $arr1;
                $arr['money'] = $v['put_number'] * $trade;
                $this->getAdd('preparereturnrelation', $arr);
                $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
                if(empty($stockInfo)){
                    $rel =[
                        'commodity_id' => $dan,
                        'product_id' => $v['product_id'],
                        'warehouse' => $data['warehouse'],
                        'stock' => 0
                    ];
                    $this->getAdd('product_stock', $rel);
                }
            }
        }
        return $arr1;
    }

    /**
     * 配货退货 详情
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail($id)
    {
        $list = $this->getOne('preparereturn','*',['id'=>$id]);
        $list['goods'] = $this->getselectList('preparereturnrelation','*',['pid'=>$list['id']]);
        foreach($list['goods'] as $k=>$v){
            $dan =$this->getOne('product','commodity_id,color,size',['id'=>$v['product_id']]);
            $goods =$this->getOne('commodity','name,item,goods_code,unit,trade_price',['id'=>$dan['commodity_id']]);
            $list['goods'][$k]['commodity_id'] =$dan['commodity_id'];
            $list['goods'][$k]['name'] =$goods['name'];
            $list['goods'][$k]['item'] =$goods['item'];
            $list['goods'][$k]['goods_code'] =$goods['goods_code'];
            $list['goods'][$k]['color'] =$this->getValue('color3',['id'=>$dan['color']],'name');
            $list['goods'][$k]['size'] =$this->getValue('color3',['id'=>$dan['size']],'name');
            $list['goods'][$k]['unit'] =$this->getValue('color1',['id'=>$goods['unit']],'name');
            $list['goods'][$k]['price'] =$goods['trade_price'];
            $list['goods'][$k]['goods_status'] =(bool) $v['goods_status'];
        }
        $list['store_name'] =$this->getValue('store',['id'=>$list['store']],'name');
        $list['warehouse_name'] =$this->getValue('warehouse',['id'=>$list['warehouse']],'name');
        $list['keeper_name'] =$this->getValue('keeper',['id'=>$list['keeper']],'name');
        $list['create_time'] = date('Y-m-d', $list['create_time']);
        $list['images'] =json_decode($list['images'],true);
        switch ($list['status']){
            case 1:
                $list['status'] = "已退货";
                break;
            case 2:
                $list['status'] = "已撤销";
                break;
            default:
                $list['status'] = "草稿";
        }
        return $list;
    }

    /*public function getReturnDepot($id)
    {
        $list = $this->getOne('preparereturn','*',['id'=>$id]);
        $list['goods'] = $this->getselectList('preparereturnrelation','*',['pid'=>$list['id']]);
        foreach($list['goods'] as $k=>$v){
            $goods =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $this->getSetInc('product_stock',['product_id'=>$v['product_id']],'stock',$v['put_number']);
            $this->getSetInc('preparereturnrelation',['id'=>$v['id']],'number',$v['put_number']);
            $this->getSetInc('warehouse_goods',['warehouse_id'=>$list['warehouse'],'goods_id'=>$goods],'stock',$v['put_number']);
            $stock =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $flow =[
                'goods_id' =>$v['product_id'],
                'create_time' =>time(),
                'warehouse' =>$list['warehouse'],
                'type' =>'配货退货单',
                'number' =>$list['number'],
                'add_num' =>$v['put_number'],
                'sur_num' =>$stock + $v['put_number']
            ];
            $this->getAdd('flowing_water',$flow);
        }
        $this->getSetField('preparereturn',['id'=>$list['id']],['status'=>1]);
        $this->getSetField('instock',['relation_id'=>$id,'type'=>5],['status'=>2]);
        return 1;
    }*/

    /**
     * 配货退货 撤销
     * @param $id
     * @return array|false|int|\PDOStatement|string|Model
     */
    public function getReturnRevoke($id)
    {
        try{
            $list =$this->getOne('preparereturn','*',['id'=>$id]);
            if(in_array($list['status'],[1,2])){
                throw new \Exception('单据已退货,无法撤销');
            }else{
                return $this->getSetField('preparereturn',['id'=>$id],['status'=>2]);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    public function returnDraft($id,$data)
    {
        $list = $this->getEdit('preparereturn',['id'=>$id],$data);
        Db::name('preparereturnrelation')->whereIn('pid',$id)->delete();
        foreach ($data['goods'] as $k=>$v){
            $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
            $relation['goods_status'] = $v['goods_status'];
            $relation['product_id'] = $v['product_id'];
            $relation['put_number'] = $v['put_number'];
            $relation['pid'] = $id;
            $relation['money'] = $relation['put_number'] * $trade;
            $this->getAdd('preparereturnrelation',$relation);
            $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
            if(empty($stockInfo)){
                $rel =[
                    'commodity_id' => $dan,
                    'product_id' => $v['product_id'],
                    'warehouse' => $data['warehouse'],
                    'stock' => 0
                ];
                $this->getAdd('product_stock', $rel);
            }
        }
        return $list;
    }

    public function prepareReturn($id,$data)
    {
        $arr =$this->getEdit('preparereturn',['id'=>$id],$data);
        Db::name('preparereturnrelation')->whereIn('pid',$id)->delete();
        $list =$this->getOne('preparereturn','*',['id'=>$id]);
        foreach ($data['goods'] as $k=>$v){
            $dan =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $trade =$this->getValue('commodity',['id'=>$dan],'trade_price');
            $relation['goods_status'] = $v['goods_status'];
            $relation['product_id'] = $v['product_id'];
            $relation['put_number'] = $v['put_number'];
            $relation['pid'] = $id;
            $relation['money'] = $relation['put_number'] * $trade;
            $this->getAdd('preparereturnrelation',$relation);
            $stockInfo =$this->getOne('product_stock','*',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']]);
            $flow =[
                'goods_id' => $v['product_id'],
                'create_time' => time(),
                'warehouse' => $data['warehouse'],
                'type' => '配货退货单',
                'number' => $list['number'],
                'add_num' => $v['put_number'],
                'sur_num' => $v['put_number']
            ];
            $this->getAdd('flowing_water',$flow);
            if(empty($stockInfo)){
                $rel =[
                    'commodity_id' => $dan,
                    'product_id' => $v['product_id'],
                    'warehouse' => $data['warehouse'],
                    'stock' => $v['put_number']
                ];
                $this->getAdd('product_stock', $rel);
            }else{
                $this->getSetInc('product_stock',['product_id'=>$v['product_id'],'warehouse'=>$data['warehouse']],'stock',$v['put_number']);
            }
        }
        $stock['status'] =3;
        $stock['number'] =$list['number'];
        $stock['create_time'] = $data['create_time'];
        $stock['warehouse'] = $data['warehouse'];
        $stock['maker'] = $data['maker'];
        $stock['type'] =2;//单据类型 2配货退货单
        $stock['des'] =$list['des'];
        $stock['relation_id'] =$id;
        $this->getAdd('instock',$stock);
        $payment =[
            'customer_id' => $data['store'],
            'customer_type' => 'store',
            'maker' => $data['maker'],
            'out_amount' => $data['total'],
            'des' => $data['des'],
            'create_time' => time(),
            'images' => $data['images'],
            'business_type' => '配货退货单',
            'business_number' => $data['number'],
            'business_time' => $data['create_time'],
        ];
        $this->getAdd('set_account_flow', $payment);
        return $arr;
    }

    /**
     * 配货退货 删除
     * @param $id
     * @return array|false|int|\PDOStatement|string|Model
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function getReturnDel($id)
    {
        try{
            $list =$this->getOne('preparereturn','*',['id'=>$id]);
            if(in_array($list['status'],[1,2])){
                throw new \Exception('单据已退货,无法删除');
            }else{
                Db::name('preparereturnrelation')->whereIn('pid',$id)->delete();
                return $this->getDel('preparereturn',$id);
            }
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }
}